package SwingController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import SwingModel.CmsUser;
import SwingView.CmsMainView;
import SwingView.CmsPageSelector.CmsPage;

public class ALCourse implements ActionListener{

	CmsMainView mainView;
	CmsUser user;
	
	//---------------------------------------------------------
	//CONSTRUCTOR:
	//---------------------------------------------------------		
	public ALCourse(CmsMainView mainView, CmsUser user){
		if(mainView == null || user == null) throw new NullPointerException();
		this.mainView = mainView;
		this.user = user;
	}
	
	//---------------------------------------------------------
	//PERFORMING ACTION:
	//---------------------------------------------------------		
	@Override
	public void actionPerformed(ActionEvent e){
		
		JComboBox<String> cb = (JComboBox) e.getSource(); //get the current combo box
		int selectedIndex = cb.getSelectedIndex();
	
		if(selectedIndex != 0){//index 0 means the "Select Course" item
			//update user selected course:
			user.setSelectedCourseIndex(selectedIndex - 1); //subtract 1 because Combo Box has extra item "Select Course"

			//update page view
			mainView.displayCoursePage(user.getSelectedCourse().getCourseID());
		}
		else{
			//TODO: update home page
			mainView.displayHomePage();
		}
	}
}
